001    /*
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: 2002-10-4
005     * Time: 16:54:10
006     * To change template for new class use 
007     * Code Style | Class Templates options (Tools | IDE Options).
008     */
009    package EVolve.util;
010    
011    import EVolve.util.equators.Set;
012    
013    import java.util.*;
014    
015    public class DataTrace implements Cloneable{
016        private ArrayList trace, stack;
017        private int invocationNum, setSize;
018        private Set currentSet;
019    
020        public DataTrace() {
021            trace = new ArrayList();
022            stack = new ArrayList();
023            invocationNum = 0;
024            currentSet = null;
025        }
026    
027        public DataTrace(Set workingSet) {
028            currentSet = workingSet;
029            trace = new ArrayList();
030            this.setSize = currentSet.size();
031            trace.add(currentSet);
032        }
033    
034        public void updateTrace(long traceId) {
035            if (currentSet.setFull()) {
036                currentSet = currentSet.newSet();
037                trace.add(currentSet);
038            }
039    
040            currentSet.addElement(traceId);
041        }
042    
043        public ArrayList getCallerChains(int callerId,int calleeId) {
044            ArrayList returnVal = new ArrayList();
045            ArrayList callerChain;
046            int footprint[];
047    
048            stack.clear();
049            for (int i = 0; i<trace.size(); i++) {
050                footprint = (int[])trace.get(i);
051            
052                if (footprint[1] == 0) {// method exit
053                    if (stack.size() != 0) stack.remove(stack.size()-1);
054                }
055                else {
056                    stack.add(new Integer(footprint[0]));
057                    if ((stack.size()>=2) &&
058                        (((Integer)stack.get(stack.size()-1)).intValue() == calleeId) &&
059                        (((Integer)stack.get(stack.size()-2)).intValue() == callerId)) {
060                        callerChain = (ArrayList)stack.clone();
061                        callerChain.remove(callerChain.size()-1);
062                        returnVal.add(callerChain);
063                    }
064                }
065    
066            }
067    
068            // remove duplicates
069            ArrayList temp = new ArrayList(), tempReturnVal = new ArrayList();
070            for (int i = 1; i< returnVal.size(); i++) {
071                temp = (ArrayList)returnVal.get(i-1);
072                boolean found = false;
073                for (int j=i; j<returnVal.size(); j++) {
074                    callerChain = (ArrayList)returnVal.get(j);
075                    if (temp.size() != callerChain.size())
076                        continue;
077                    for (int k=0; k< temp.size(); k++) {
078                        if (((Integer)temp.get(k)).intValue() != ((Integer)callerChain.get(k)).intValue()) {
079                            found = false;
080                            break;
081                        }
082                        found = true;
083                    }
084                    if (found) break;
085                }
086                if (!found)
087                    tempReturnVal.add(temp);
088            }
089            if (tempReturnVal.size()>0)
090                returnVal = tempReturnVal;
091            return returnVal;
092        }
093        
094        public ArrayList getCallerChain(int callerId, int calleeId) {
095            ArrayList callerChain = new ArrayList();
096            int[] footprint;
097    
098            callerChain.add(new Integer(callerId));
099            stack.clear();
100            for (int i = 0; i<trace.size(); i++) {
101                footprint = (int[])trace.get(i);
102    
103                if (footprint[1] == 0) { // method exit event
104                    if (stack.size() != 0) stack.remove(stack.size()-1);
105                } else {
106                    stack.add(new Integer(footprint[0]));
107                    if ((stack.size()>=2) &&
108                        (((Integer)stack.get(stack.size()-1)).intValue() == calleeId) &&
109                        (((Integer)stack.get(stack.size()-2)).intValue() == callerId)) {
110                        callerChain = (ArrayList)stack.clone();
111                        callerChain.remove(callerChain.size()-1);
112                        break;
113                    }
114                }
115            }
116    
117            return callerChain;
118        }
119    
120        public ArrayList getCallees(int callerId, int calleeId) {
121            ArrayList callees = new ArrayList();
122            int footprint[],i;
123    
124            stack.clear();
125            for (i = 0; i<trace.size(); i++) {
126                footprint = (int[])trace.get(i);
127    
128                if (footprint[1] == 0) { // method exit event
129                    if (stack.size() != 0) stack.remove(stack.size()-1);
130                } else {
131                    stack.add(new Integer(footprint[0]));
132                    if ((stack.size()>=3) &&
133                        (((Integer)stack.get(stack.size()-2)).intValue() == calleeId) &&
134                        (((Integer)stack.get(stack.size()-3)).intValue() == callerId)) {
135                        callees.add(new Integer(footprint[0]));
136                    }
137                }
138            }
139    
140            // remove duplicates
141            ArrayList temp = new ArrayList();
142            for (i=0; i<callees.size();i++) {
143                int value = ((Integer)callees.get(i)).intValue();
144                boolean found = false;
145                if (i==0) {
146                    temp.add(new Integer(value));
147                    continue;
148                }
149                for (int j=0; j<temp.size();j++) {
150                    if (value == ((Integer)temp.get(j)).intValue()) {
151                        found = true;
152                        break;
153                    }
154                }
155                if (!found)
156                    temp.add(new Integer(value));
157            }
158            callees = temp;
159            return callees;
160        }
161    
162        public int size() {
163            return trace.size();
164        }
165    
166        public ArrayList getTrace() {
167            return trace;
168        }
169    
170        public Object clone() {
171            DataTrace o = null;
172            try {
173                o = (DataTrace)super.clone();
174            } catch (CloneNotSupportedException e) {
175                e.printStackTrace();
176                return null;
177            }
178            o.trace = (trace == null) ? null :(ArrayList)trace.clone();
179            o.stack = (stack == null)? null : (ArrayList)stack.clone();
180            o.currentSet = (currentSet == null) ? null : (Set)currentSet.clone();
181            return o;
182        }
183    }